<?php
/**
 *山西酷源网络科技有限公司
 * @author Louie
 */
defined('IN_IA') or exit('Access Denied');
//开发模式
define('KUYUAN_DEBUG',true);
KUYUAN_DEBUG ? error_reporting(E_ALL & ~E_NOTICE) : error_reporting(0);

//验证token
define("KUYUAN_TOKEN","shanxikuyuanwangluokeji");

/**
 * 执行类
 * @param $_className
 * @return mixed
 */
function execClass($_className)
{
    return new $_className();
}

/**
 * 执行方法
 * @param $_class
 * @param $_methodName
 */
function execMethod($_class,$_methodName)
{
    $_class->$_methodName();
}

class kuyuan_stallModuleWxapp extends WeModuleWxapp {

    public function doPageApi()
    {
        $this->api();
    }

    private function api()
    {
        require MODULE_ROOT.'/vendor/autoload.php';
        global $_GPC;
        if($_GPC['sign'] != $this->getSign()){
            $this->result(-1,array("status"=>"error","err_status"=>-1,"err_msg"=>"sign错误"));
        }
        if(isset($_GPC['pages']) && !empty($_GPC['pages'])){
            if(!is_file(MODULE_ROOT.'/'.$_GPC['kv'].'/'.ucwords($_GPC['pages']).'.php')){
                exit(ucwords($_GPC['pages']).'.php文件不存在');
            }
            $_action = call_user_func('execClass','Api\\'.ucwords($_GPC['pages']));
            if(isset($_GPC['doing']) && !empty($_GPC['doing'])){
                $_methodName = $_GPC['doing'];
            }else{
                $_methodName = 'index';
            }
            if(!method_exists($_action,$_methodName)){
                exit($_methodName.'()方法不存在');
            }
            call_user_func('execMethod',$_action,$_methodName);
        }
    }

    /**
     * 验证sign
     */
    private function getSign()
    {
        $_post_data = $_POST;
        unset($_post_data['sign']);
        $_key_arr = array_keys($_post_data);
        sort($_key_arr,SORT_STRING);
        $_key_str = '';
        foreach($_key_arr as $_v){
            $_key_str .= $_v;
        }
        $_key_str .= KUYUAN_TOKEN;
        return strtoupper(md5($_key_str));
    }
}